# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from reportlab.lib import colors
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table
from reportlab.lib.styles import getSampleStyleSheet
import time


class BillPDF():
	def __init__(self, path=None, log=None, bill=None, lines=None):
		self.path=path
		self.log=log
		self.bill=bill
		self.lines=lines
		self.check=0
		self.log.info("\tBillPDF\tStart")
		self.pdfPage()
	
	def pdfPage(self):
		# Container for 'Flowable' objects
		elements = []
		
		# Collection of style sheets pre-made for us
		styles = getSampleStyleSheet()
		
		# Basic document for us to write to 'SortReference_datetime.pdf'
		dt=time.strftime("%Y%m%d%H%M" ,time.localtime())
		doc = SimpleDocTemplate("pdf/"+self.bill.sort+self.bill.reference+"_"+dt+".pdf")
		
		# Company coordinate
		company=[	[str(self.bill.place.company)],
							[str(self.bill.place.address1)],
							[str(self.bill.place.city.zipCode.code)+" - "+str(self.bill.place.city.name)],
							["Tel: "+str(self.bill.place.tel)],
							["Fax: "+str(self.bill.place.fax)] ]
		
		tableCompany=Table(company)
		## elements.append(tableCompany)
		
		# Customer coordinate
		customer=[	[str(self.bill.customer.fname)+" "+str(self.bill.customer.lname)],
							[str(self.bill.customer.address1)],
							[str(self.bill.customer.city.zipCode.code)+" - "+str(self.bill.customer.city.name)],
							["Tel: "+str(self.bill.customer.tel)] ]
							
		tableCustomer=Table(customer)
		## elements.append(tableCustomer)
		
		# Order reference
		if self.bill.sort == "C":
			sortRef=Paragraph("Commande "+self.bill.reference, styles["Title"])
			infoRef=Paragraph("Valable 30 jours", styles["Normal"])
		elif self.bill.sort == "D":
			sortRef=Paragraph("Devis "+self.bill.reference, styles["Title"])
			infoRef=Paragraph("Valable 30 jours", styles["Normal"])
		elif self.bill.sort == "F":
			sortRef=Paragraph("Facture "+self.bill.reference, styles["Title"])
			infoRef=Paragraph("Payable avant le "+self.bill.dueDate.strftime("%d/%m/%Y"), styles["Normal"])
		elif self.bill.sort == "A":
			sortRef=Paragraph("Avoir "+self.bill.reference, styles["Title"])
			infoRef=Paragraph("Valable une année", styles["Normal"])
		
		reference=[ [sortRef], ["Date :"+self.bill.date.strftime("%d/%m/%Y")], [infoRef] ]
		tableReference=Table(reference)
		## elements.append(tableReference)
		
		# Header col Order
		headerColOrder=[u"N°", u"Référence", u"Désignation", u"Unitaire HT", u"Quantité", u"Prix HT",u"Taxe", u"Prix TTC"]
		
		# Lines Order
		linesOrder=[]
		linesOrder.append(headerColOrder)
		
		for line in self.lines:
			linesOrder.append([str(line.line), str(line.reference), str(line.designation), "%.2f" %line.price, "%.2f" %line.quantity, "%.2f" %line.totalPrice, "%.2f" %line.tax.rate, "%.2f" %line.priceInclusTax ])
		
		tableLinesOrder=Table(linesOrder)
		## elements.append(tableLinesOrder)
		
		# Total Order
		totalOrder=[	["Total HT : ", "%.2f"%self.bill.tHTPrice],
								["Total Taxes : ", "%.2f"%self.bill.tTaxPrice],
								["Total TTC : ", "%.2f"%self.bill.tTCPrice] 	]
		
		tableTotalOrder=Table(totalOrder)
		## elements.append(tableTotalOrder)
		
		header=[	[tableCompany, " "], [" ", tableCustomer] ]
		tableHeader=Table(header)
		
		total=[	[" ", tableTotalOrder] ]
		tableTotal=Table(total)
		
		page=[	[tableHeader], [tableReference], [tableLinesOrder], [tableTotal] ]
		tablePage=Table(page)
		elements.append(tablePage)
		
		# write the document to disk
		doc.build(elements)
		
		self.check=1
	